<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>二进制转十进制</title>
</head>
<body>
<script type="application/javascript">
    function twoForTen (twoCodeStr) {
      let twoCodeArray = twoCodeStr.split('').reverse()
      let array = []
      twoCodeArray.forEach((item, index) => {
        if (item === '1') {
          array.push(index)
        }
      })
      let res = 0
      array.forEach(item => {
        res += Math.pow(2, item)
      })
      console.log(res)
    }
    twoForTen('1010')
</script>
</body>
</html>
